package edu.gatech.cs4220;

/**
 * A point designating the center of a area for a location profile.
 * A VERY stripped down version of the {@link android.location.Location} class.
 * Also, unlike Location objects, these are immutable.  
 * @author C. Sean Young
 *
 */
final class LocationProfilePoint implements java.io.Serializable
{
	private static final long serialVersionUID = 8324828522650496922L;
	
	//Package private, so we are safe to expose these and use this like a struct
	final double latitude;
	final double longitude;
	final float accuracy;
	
	transient String toStringCache = null;

	LocationProfilePoint(double latitude, double longitude, float accuracy)
	{
		this.latitude = latitude;
		this.longitude = longitude;
		this.accuracy = accuracy;
	}
	
	LocationProfilePoint(android.location.Location l)
	{
		this.latitude = l.getLatitude();
		this.longitude = l.getLongitude();
		this.accuracy = l.getAccuracy();
	}

	@Override
	public boolean equals(Object obj)
	{
		if(this == obj) return true;
		if(!(obj instanceof LocationProfilePoint)) return false;
		LocationProfilePoint other = (LocationProfilePoint)obj;
		return 
			Float.floatToIntBits(accuracy) == Float.floatToIntBits(other.accuracy) &&
			Double.doubleToLongBits(latitude) == Double.doubleToLongBits(other.latitude) &&
		    Double.doubleToLongBits(longitude) == Double.doubleToLongBits(other.longitude);
	}
	
	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(accuracy);
		long temp;
		temp = Double.doubleToLongBits(latitude);
		result = prime * result + (int)(temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(longitude);
		result = prime * result + (int)(temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public String toString()
	{
		//As these are immutable, use lazy caching, to avoid a dumb concatenation each time
		if(toStringCache == null)
			toStringCache = "LocationProfilePoint [accuracy=" + accuracy + ", latitude=" + latitude
				+ ", longitude=" + longitude + "]";
		return toStringCache;
	}
	
	static LocationProfilePoint getInstance(android.location.Location l)
	{
		return new LocationProfilePoint(l.getLatitude(), l.getLongitude(), l.getAccuracy());
	}
}
